package com.maoyan.quickdevelop.system.service.Impl;

import cn.dev33.satoken.secure.SaSecureUtil;
import cn.dev33.satoken.stp.SaTokenInfo;
import cn.dev33.satoken.stp.StpUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.maoyan.quickdevelop.common.utils.StringUtils;
import com.maoyan.quickdevelop.common.constant.HttpStatus;
import com.maoyan.quickdevelop.common.core.domain.DqUser;
import com.maoyan.quickdevelop.common.core.exception.CustomException;
import com.maoyan.quickdevelop.common.utils.DateUtils;
import com.maoyan.quickdevelop.system.domain.vo.LoginVO;
import com.maoyan.quickdevelop.system.mapper.DqUserMapper;
import com.maoyan.quickdevelop.system.service.IDqLoginService;
import com.maoyan.quickdevelop.system.service.IDqUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Transactional
@Service
@Slf4j
public class IDqLoginServiceImpl implements IDqLoginService {

  @Autowired
  private IDqUserService iUserService;
  @Autowired
  private DqUserMapper dqUserMapper;

  @Override
  public SaTokenInfo dqUserLogin(LoginVO loginVO) {
    //登陆方法
    DqUser dqUser = iUserService.selectDqUserByUserNameAndPassword(loginVO.getUserName()
            , SaSecureUtil.md5(SaSecureUtil.sha1(loginVO.getPassWord())));
    //去掉用户的password
    dqUser.setPassWord("000000");
    /** 断言适合做测试开发，不适合实际生产 **/
    //Assert.notNull(dqUser,"用户名密码错误");
    //Sa-Token登陆
    //TODO 登陆并使用数据库用户ID来作为Satoken的用户ID
    StpUtil.setLoginId(dqUser.getUserId(), true);
//        StpUtil.setLoginId(dqUser.getUserName(),true);
    SaTokenInfo tokenInfo = StpUtil.getTokenInfo();
    System.out.println(tokenInfo.tokenName);
    System.out.println(tokenInfo.tokenValue);
    //传入tokeninfo,不再使用传统的cookie
//        		"tokenName": "MaoyanToken",
//		"tokenValue": "e4fd232e-c5cf-4f00-afa7-f3d4875ede6b
    return tokenInfo;
  }

}
